﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;

namespace EHTWebsiteProject.Administrator
{
    public partial class AdminFAQs : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";      
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));        
        private Int32 pageIndex = 1;
        private Int32 pageSize = 20;
        private Int32 totalRecord;      
        private Int32 TypeFAQs = HangSo.DEFAULT_ID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(EHTWebsiteProject.Administrator.AdminFAQs));
                if (Session["UserName"] == null)
                    Response.Redirect("../Account/EHTLogin.aspx");
                else
                {
                    try
                    {
                        languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                        if (languageCode == "en-us")
                        {
                            IsVN = false;
                        }

                    }
                    catch (Exception ex)
                    {
                    }

                    if (Request.QueryString["TypeFAQs"] == null)
                        TypeFAQs = HangSo.DEFAULT_ID;
                    else
                    {
                        TypeFAQs = Int32.Parse(Request.QueryString["TypeFAQs"].ToString());
                    }
                    Session["TypeFAQs"] = TypeFAQs.ToString();                   

                    try
                    {
                        pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
                    }
                    catch
                    {
                        pageIndex = 1;
                    }

                    Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                    LoadLanguage(Thread.CurrentThread.CurrentCulture);
                    loadFAQs(Int32.Parse(Session["TypeFAQs"].ToString()));
                }
            }
        }


        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbAddNew.Text = rm.GetString("lbAddNew", ci);
            lbDel.Text = rm.GetString("lbDel", ci);
            lbEdit.Text = rm.GetString("lbEdit", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbFAQs.Text = rm.GetString("lbFAQs", ci);

        }

        public void loadFAQs(Int32 TypeFAQs)
        {
            rpFAQs.DataSource = FAQsProvider.GetFAQs();
            rpFAQs.DataBind();

            //phân trang 
            this.PaggingControl1.PageIndex = pageIndex;
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PageURL = "AdminFAQs.aspx?Type=" + TypeFAQs.ToString();
        }

        protected void ImgDel_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();           
                foreach (RepeaterItem rp in rpFAQs.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }           

            for (int i = 0; i < arr.Count; i++)
                FAQsProvider.DeleteFAQs(Int32.Parse(arr[i].ToString()));
            loadFAQs(Int32.Parse(Session["TypeFAQs"].ToString()));
        }

        protected void ImgEdit_Click(object sender, ImageClickEventArgs e)
        {
            Int32 ID = 0;
            Int32 Count = 0;            
                foreach (RepeaterItem rp in rpFAQs.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        ID = Int32.Parse(lbDelID.Text);
                        Count++;
                    }
                }            
            if (Count > 1 || Count == 0)
            {
                String messa = "";
                if (IsVN == true)
                    messa = HangSo.Eorr_Edit;
                else
                    messa = HangSo.Eorr_EditOther;
                ltrErorr.Text = HelpMethod.ErorrLogin(messa);
            }
            else
            {
                Response.Redirect("AdminFAQsUI.aspx?ID=" + ID.ToString());
            }
        }

        protected void ddlFAQs_SelectedIndexChanged(object sender, EventArgs e)
        {
            int type = Int32.Parse(ddlFAQs.SelectedValue.ToString());
            if (type != -1)
            {
                rpFAQs.DataSource = FAQsProvider.GetFAQsByType(type, true);
                rpFAQs.DataBind();
            }
            else
            {
                rpFAQs.DataSource = FAQsProvider.GetFAQs();
                rpFAQs.DataBind();
            }
        }

        /// <summary>
        /// Ham check all hoặc uncheck all
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CheckAll(object sender, System.EventArgs e)
        {
            CheckBox ckAll = (CheckBox)sender;
                         
                if (ckAll.Checked)
                {
                    //duyệt mảng
                    foreach (RepeaterItem rp in rpFAQs.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpFAQs.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }            
        }


    }
}